package com.example.demo.controller;
import com.deepoove.poi.XWPFTemplate;
import com.deepoove.poi.config.Configure;
import com.deepoove.poi.policy.HackLoopTableRenderPolicy;
import com.example.demo.common.Result;
import com.example.demo.entity.EO;
import com.example.demo.mapper.EoMapper;
import com.example.demo.service.EoService;
import com.example.demo.service.impl.EoServiceImpl;
import org.apache.xmlbeans.ResourceLoader;
import org.ddr.poi.html.HtmlRenderPolicy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/eo")
public class EoController extends BaseController{

    @Resource
    EoMapper eoMapper;

    @Autowired
    EoServiceImpl eoService;

    @PostMapping("/add")
    public Result<?> save(@RequestBody EO eo, HttpServletRequest request) {
        return Result.success(eoService.add(eo, request));
    }

    @PostMapping("/update")
    public Result<?> update(@RequestBody EO eo, HttpServletRequest request) {
        return Result.success(eoService.update(eo, request));
    }

    @DeleteMapping("/delete/{id}")
    public Result<?> delete(@PathVariable Long id) {
        return Result.success(eoService.delete(id));
    }

    @GetMapping("/list")
    public Result<?> list(@RequestParam(defaultValue = "1") Integer pageNum,
                          @RequestParam(defaultValue = "10") Integer pageSize,
                          @RequestParam(defaultValue = "") String search,
                          @RequestParam(defaultValue = "") String month,
                          @RequestParam(defaultValue = "") String departmentId,
                          @RequestParam Integer peopleId,
                          @RequestParam(defaultValue = "") String status,
                          @RequestParam(defaultValue = "") String origin,
                          @RequestParam(defaultValue = "") String type){
        return Result.success(eoService.list(pageNum, pageSize, search, month, departmentId, peopleId, status, origin, type));
    }

    @GetMapping("/select/{id}")
    public Result<?> select(@PathVariable Long id){
        return Result.success(eoMapper.selectById(id));
    }

    @PostMapping("/synchronise")
    public Result<?> synchronise(HttpServletRequest request) throws Exception{
        return Result.success(eoService.synchronise(request));
    }

    @GetMapping("/download/{id}")
    public void download(@PathVariable Long id, HttpServletResponse response){
        EO eo = eoMapper.selectById(id);
        String path = "D:/system/system-back/files/template/";
        if (eo.getType().equals("活动")){
            path = path + "activity_template.docx";
        } else {
            path = path + "reception_template.docx";
        }
        OutputStream out = null;
        try{
            Map<String, Object> map = eoService.getMap(id);

            HackLoopTableRenderPolicy policy = new HackLoopTableRenderPolicy();
            HtmlRenderPolicy htmlPolicy = new HtmlRenderPolicy();
            Configure config = Configure.newBuilder()
                    .bind("timeList", policy)
                    .bind("route", htmlPolicy)
                    .bind("schedule", htmlPolicy)
                    .bind("peopleList", policy)
                    .bind("content", htmlPolicy)
                    .bind("guest_commit", htmlPolicy)
                    .bind("time_text", htmlPolicy)
                    .bind("leaderList", policy)
                    .build();
            XWPFTemplate template = XWPFTemplate.compile(path, config).render(map);
            response.setContentType("application/vnd.ms-excel;charset=gb2312");
            response.setHeader("Content-disposition","attachment;filename=" + URLEncoder.encode(eo.getTitle() + ".docx", "UTF-8"));
//            response.addHeader("Access-Control-Expose-Headers", "Content-disposition");
            out = response.getOutputStream();
            template.write(out);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(out != null){
                try {
                    out.flush();
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
